<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_validation {

	public function __construct($rules = array())
	{
		parent::__construct($rules);
	}

	public function unique($value, $params) {

		$CI =& get_instance();
		$CI->load->database();

		$CI->form_validation->set_message('unique',
			'The %s is already being used.');

		list($table, $field) = explode(".", $params, 2);

		$query = $CI->db->select($field)->from($table)
			->where($field, $value)->limit(1)->get();

		if ($query->row()) {
			return FALSE;
		} else {
			return TRUE;
		}

	}

	/**
	 * Return validated fields as assoc array
	 *
	 * @return array 
	 */
	public function get_validated_data()
	{
			$data = array();
			foreach($this->_field_data as $key => $value)
			{
					$key = str_replace(array('[',']'),'',$key);
					$data[$key] = $value['postdata'];
			}
			return $data;
	}

	/**
	 * alnum + comma
	 *
	 * @access        public
	 * @param        string
	 * @return        bool
	 */
	function letters_numbers_comma($str)
	{
			return ( ! preg_match("/^([a-z0-9, ])+$/i", $str)) ? FALSE : TRUE;
	}

}
